package webflux;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@SpringBootApplication
@RestController
public class WebFluxApp {
    @Autowired
    private AccountRespository accountRespository;

    public static void main(String[] args) {
        SpringApplication.run(WebFluxApp.class,args);
    }

    @GetMapping("/accounts")
    public Flux<Account> findAllAccounts(){
        return Flux.create(fluxSink -> {
            for (int i = 0; i < 3; i++) {
                Account account = new Account().setId(i).setName("tom" + i);
                fluxSink.next(account);
            }
            fluxSink.complete();//必须加上，否则Flux不会终止
        });
    }

    @GetMapping("/accounts2")
    public ResponseEntity<Flux<Account>> findAllAccounts2(){
      return   ResponseEntity.ok(accountRespository.findAll());
    }
}
